package org.future.task.core.monitor;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.future.task.core.cache.ParamCache;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author Dongwei-Chen
 * @Date 2019/5/28 15:18
 * @Description http服务展示
 */
public class HttpHandlerShow implements HttpHandler {

    private static String projectName;

    @Override
    public void handle(HttpExchange httpExchange) throws IOException {
        httpExchange.sendResponseHeaders(200, 0);
        OutputStream os = httpExchange.getResponseBody();
        if (projectName == null) {
            String project = "ceshi";
            projectName =
                    project.substring(0, project.length() > 14 ? 14 : project.length());
        }

        StringBuilder queue = new StringBuilder();

        ParamCache.getConnectionMaps().forEach((k, v) -> {
            queue.append("        <tr>\n")
                    .append("          <td>").append(k).append("</td>\n")
                    .append("          <td>").append(v.size(k)).append("</td>\n")
                    .append("          <td>").append(v.getType()).append("</td>\n")
                    .append("          <td>").append(ParamCache.getTask().get(k).size()).append("</td>\n")
                    .append("        </tr>\n");
        });

        StringBuilder running = new StringBuilder();

        ParamCache.getTask().forEach((k, v) -> {
            for (Thread thread : v) {
                running.append("        <tr>\n")
                        .append("          <td>").append(k).append("</td>\n")
                        .append("          <td>").append(thread.getName()).append("</td>\n")
                        .append("          <td>").append(ParamCache.getFlows().get(thread.getName())).append("</td>\n")
                        .append("        </tr>\n");
            }
        });

        os.write(("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "<meta http-equiv=\"refresh\" content=\"50\">\n" +
                "  <meta charset=\"UTF-8\">\n" +
                "  <title>" + (projectName == null ? "工作线程监控" : projectName) + "</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div class=\"box\">\n" +
                "  <div class=\"form-head\">\n" +
                "    <div class=\"head-title\">\n" +
                "      <div class=\"title\" style=\"overflow:hidden;\">" + projectName + "-工作线程监控</div>\n" +
                "    </div>\n" +
                "    <div class=\"head-box\">\n" +
                "      <div class=\"head-box-count\">\n" +
                "        <div class=\"total\">运行线程总数:" + "" + "</div>\n" +
                "      </div>\n" +
                "      <div class=\"head-box-left\">\n" +
                "        <div class=\"running\">Running : " + ("") + "</div>\n" +
                "      </div>\n" +
                "      <div class=\"head-box-right\">\n" +
                "        <div class=\"waiting\">Waiting : " + "" + "</div>\n" +
                "      </div>\n" +
                "    </div>\n" +
                "  </div>\n" +
                "  <div class=\"queue-size\">\n" +
                "    <div class=\"queue-left\">\n" +
                "      <table border=\"1\">\n" +
                "        <thead>\n" +
                "        <tr>\n" +
                "          <th>队列名</th>\n" +
                "          <th>数据量</th>\n" +
                "          <th>队列类型</th>\n" +
                "          <th>运行线程数</th>\n" +
                "        </tr>\n" +
                "        </thead>\n" +
                "        <tbody>\n" +
                queue.toString() +
                "        </tbody>\n" +
                "      </table>\n" +
                "    </div>\n" +
                "    <div class=\"queue-right\">\n" +
                "\n" +
                "      <table border=\"1\">\n" +
                "        <thead>\n" +
                "        <tr>\n" +
                "          <th>数据队列名</th>\n" +
                "          <th>运行中线程</th>\n" +
                "          <th>执行速度(次/s)</th>\n" +
                "        </tr>\n" +
                "        </thead>\n" +
                "        <tbody>\n" +
                running.toString() +
                "        </tbody>\n" +
                "      </table>\n" +
                "    </div>\n" +
                "  </div>\n" +
                "  <div class=\"worker-plate\">\n" +
                "" +
                "  </div>\n" +
                "  <style>\n" +
                "    body {\n" +
                "      background-color: white;\n" +
                "    }\n" +
                "\n" +
                "    table, tbody {\n" +
                "      display: block;\n" +
                "      height: 195px;\n" +
                "      border: 0;\n" +
                "      border-spacing: 0;\n" +
                "      border-collapse: collapse;\n" +
                "      cursor: default;\n" +
                "    }\n" +
                "\n" +
                "    tbody {\n" +
                "      height: 577px;\n" +
                "      overflow-y: scroll;\n" +
                "    }\n" +
                "\n" +
                "    table thead,\n" +
                "    tbody tr {\n" +
                "      display: table;\n" +
                "      width: 100%;\n" +
                "      table-layout: fixed;\n" +
                "    }\n" +
                "\n" +
                "    table thead {\n" +
                "      width: calc(100% - 0em)\n" +
                "    }\n" +
                "\n" +
                "    table thead th {\n" +
                "      background: #84acde;\n" +
                "      width: 20%;\n" +
                "    }\n" +
                "\n" +
                "    table tbody td {\n" +
                "      width: 20%;\n" +
                "      font-size: 14px;\n" +
                "      overflow:scroll;\n" +
                "      text-align: center;\n" +
                "    }\n" +
                "\n" +
                "    ::-webkit-scrollbar {\n" +
                "      width: 2px; /*对垂直流动条有效*/\n" +
                "      height: 2px; /*对水平流动条有效*/\n" +
                "    }\n" +
                "\n" +
                "    /*定义滚动条的轨道颜色、内阴影及圆角*/\n" +
                "    ::-webkit-scrollbar-track {\n" +
                "      -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);\n" +
                "      background-color: #3eecff;\n" +
                "      border-radius: 3px;\n" +
                "    }\n" +
                "\n" +
                "    /*定义滑块颜色、内阴影及圆角*/\n" +
                "    ::-webkit-scrollbar-thumb {\n" +
                "      border-radius: 7px;\n" +
                "      -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);\n" +
                "      background-color: #E8E8E8;\n" +
                "    }\n" +
                "\n" +
                "    /*定义两端按钮的样式*/\n" +
                "    ::-webkit-scrollbar-button {\n" +
                "      background-color: #aff5ff;\n" +
                "    }\n" +
                "\n" +
                "    /*定义右下角汇合处的样式*/\n" +
                "    ::-webkit-scrollbar-corner {\n" +
                "      background: #aff5ff;\n" +
                "    }\n" +
                "\n" +
                "    .box {\n" +
                "      width: 95%;\n" +
                "      height: auto;\n" +
                "      margin: 0 auto;\n" +
                "      border-radius: 5px;\n" +
                "      outline: 1px solid #aff5ff;\n" +
                "    }\n" +
                "\n" +
                "    .form-head {\n" +
                "      width: 100%;\n" +
                "      height: 200px;\n" +
                "      margin: 0 auto 3px auto;\n" +
                "      border-radius: 5px;\n" +
                "    }\n" +
                "\n" +
                "    .head-title {\n" +
                "      width: 100%;\n" +
                "      height: 40px;\n" +
                "      outline: 1px solid #b4b4b4;\n" +
                "      margin-bottom: 3px;\n" +
                "      border-bottom: 1px dotted #b4b4b4;\n" +
                "      border-radius: 5px;\n" +
                "      background-color: white;\n" +
                "      font-size: 18px;\n" +
                "      font-weight: 500;\n" +
                "      font-family: Candara;\n" +
                "    }\n" +
                "\n" +
                "    .title {\n" +
                "      width: 400px;\n" +
                "      height: inherit;\n" +
                "      text-align: center;\n" +
                "      line-height: 40px;\n" +
                "      margin: 0 auto;\n" +
                "    }\n" +
                "\n" +
                "    .head-box {\n" +
                "      width: 100%;\n" +
                "      height: 158px;\n" +
                "      border-radius: 5px;\n" +
                "      background-color: #2f79ff;\n" +
                "    }\n" +
                "\n" +
                "    .head-box-count {\n" +
                "      width: 100%;\n" +
                "      height: 50px;\n" +
                "      margin: 0 auto;\n" +
                "      border-bottom: 1px dotted #feffff;\n" +
                "    }\n" +
                "\n" +
                "    .total {\n" +
                "      width: 100%;\n" +
                "      height: inherit;\n" +
                "      text-align: center;\n" +
                "      line-height: 50px;\n" +
                "      font-weight: 900;\n" +
                "      font-size: 25px;\n" +
                "      color: white;\n" +
                "    }\n" +
                "\n" +
                "    .head-box-left {\n" +
                "      width: 50%;\n" +
                "      height: 108px;\n" +
                "      float: left;\n" +
                "    }\n" +
                "\n" +
                "    .running {\n" +
                "      width: 100%;\n" +
                "      height: inherit;\n" +
                "      text-align: center;\n" +
                "      line-height: 108px;\n" +
                "      font-weight: 900;\n" +
                "      font-size: 25px;\n" +
                "      color: lawngreen;\n" +
                "    }\n" +
                "\n" +
                "    .waiting {\n" +
                "      width: 100%;\n" +
                "      height: inherit;\n" +
                "      text-align: center;\n" +
                "      line-height: 108px;\n" +
                "      font-weight: 900;\n" +
                "      font-size: 25px;\n" +
                "      color: orange;\n" +
                "    }\n" +
                "\n" +
                "    .head-box-right {\n" +
                "      width: 50%;\n" +
                "      height: 108px;\n" +
                "      float: right;\n" +
                "    }\n" +
                "\n" +
                "    .queue-size {\n" +
                "      width: 100%;\n" +
                "      height: 600px;\n" +
                "      margin: 4px auto 0 auto;\n" +
                "      outline: 1px solid blanchedalmond;\n" +
                "      background-color: white;\n" +
                "      border-radius: 5px;\n" +
                "    }\n" +
                "\n" +
                "    .queue-left {\n" +
                "      width: 50%;\n" +
                "      height: inherit;\n" +
                "      float: left;\n" +
                "    }\n" +
                "\n" +
                "    .queue-right {\n" +
                "      width: 50%;\n" +
                "      height: inherit;\n" +
                "      float: right;\n" +
                "    }\n" +
                "\n" +
                "    .worker-plate {\n" +
                "      width: 100%;\n" +
                "      height: auto;\n" +
                "      margin: 0 auto;\n" +
                "    }\n" +
                "\n" +
                "    .plate {\n" +
                "      width: 25%;\n" +
                "      height: 600px;\n" +
                "      float: left;\n" +
                "      outline: 1px solid orange;\n" +
                "    }\n" +
                "  </style>\n" +
                "</div>\n" +
                "</body>\n" +
                "</html>\n").getBytes("utf-8"));

        os.close();
    }

    private String formatRunTime(Long runTime) {
        if (runTime == null) {
            return "--/--/--";
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(new Date(runTime));
        } catch (Exception e) {
        }
        return "--/--/--";
    }
}
